<template>
  <div class="dashboard-container">
    <div class="departments-container">
      <!-- 卡片组件(分为头部和身体) -->
      <el-card class="box-card">
        <!-- 头部 -->
        <div slot="header" class="clearfix">
          <!-- tab导航 -->
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织架构" name="first" class="tab-pane">
              <el-row type="flex" justify="space-between" align="middle" class="department-header">
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown>
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="add()">添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
                <!-- 默认插槽  并且使用内部的数据进行自定义模板的渲染, scope.data 所以直接key解构赋值变量也是data(对应数据对象)-->
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%;"
                  >
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <!-- 功能操作区域 -->
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="add(data)">添加子部门</el-dropdown-item>
                              <el-dropdown-item @click.native="edit(data)">编辑部门</el-dropdown-item>
                              <el-dropdown-item v-if="data&&!data.children" @click.native="del(data)">删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>

            </el-tab-pane>
          </el-tabs>
        </div>

      </el-card>
      <depart-dialog-vue
        ref="dialogDepart"
        :dialog-visible="dialogVisible"
        :employees-simple-list="EmployeesSimpleList"
        :origin-list="originList"
        :departments-id="departmentsId"
        :is-edit="isEdit"
        @showDialog="showDialogFn"
        @addDepartEV="addDepartFn"
      />
    </div>
  </div>
</template>

<script>
import { getdepartmentsAPI, getEmployeesSimpleAPI, addDepartmentsAPI, getDepartDetailAPI, updateDepartDetailAPI, delDepartAPI } from '@/api'// 导入接口
import { transTree } from '@/utils/index'// 导入封装的数据方法
import departDialogVue from './components/departDialog.vue'// 导入departDialog组件
export default {
  name: 'Departments',
  components: {
    departDialogVue
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      // 树形控件数据
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'departName'
      },
      dialogVisible: false,
      // 员工简单列表数据
      EmployeesSimpleList: [],
      // 设置变量接收部门id
      departmentsId: '',
      // 设置变量判断新增和编辑，true为新增，false为编辑
      isEdit: true,
      // 用于扁平数据
      originList: []

    }
  },
  created() {
    // 获取企业部门信息
    this.getdepartmentsFn()
    // 获取员工简单信息
    this.getEmployeesSimpleFn()
  },
  methods: {

    // 获取企业基本信息方法
    async getdepartmentsFn() {
      const res = await getdepartmentsAPI()
      // console.log(res)
      this.originList = res.data.depts.map(item => (
        {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid // 下面使用
        }
      ))
      this.treeData = transTree(res.data.depts, '')
    },
    // 获取员工简单信息
    async getEmployeesSimpleFn() {
      const res = await getEmployeesSimpleAPI()
      // console.log(res)
      this.EmployeesSimpleList = res.data
    },

    handleClick(tab, event) {
      console.log(tab, event)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    showDialogFn(bool) {
      this.dialogVisible = bool
    },
    // 正文部分-右侧的添加子部门
    add(data) {
      this.isEdit = true
      this.dialogVisible = true
      if (data === undefined) {
        this.departmentsId = ''
      } else {
        this.departmentsId = data.id
      }

      this.$refs.dialogDepart.form = {}
    },
    // 编辑子部分
    async edit(data) {
      this.isEdit = false
      this.dialogVisible = true
      this.departmentsId = data.id
      const res = await getDepartDetailAPI(data.id)
      // console.log(res)
      this.$refs.dialogDepart.form = res.data
    },
    // 删除部分
    del(data) {
      // console.log(res)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartAPI(data.id)
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getdepartmentsFn()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击确定=>新增/编辑部门信息
    async addDepartFn(formObj) {
      if (this.isEdit) {
        formObj.pid = this.departmentsId
        await addDepartmentsAPI(formObj)
      } else {
        await updateDepartDetailAPI(formObj)
      }

      this.getdepartmentsFn()
    }
  }
}

</script>

  <style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
